//
//  VistorView.swift
//  LEOweibo
//
//  Created by zz on 16/2/24.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit

import SnapKit
class VistorView: UIView {
    //MARK:- 生命周期函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
        startAnimation()
        
        loginButton.addTarget(self, action: "didClickLoginButton", forControlEvents: .TouchUpInside)
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        print("访客视图deinit")
    }
    
    //MARK:- 监听方法
    //登录按钮点击实现的方法
    @objc func didClickLoginButton (){
        
        NSNotificationCenter.defaultCenter().postNotificationName(LeoDidClickLoginButton, object: nil)
        
    }
    
    //MARK:- 公共方法
    ///设置访客视图信息
    func setUpInfo(text: String, imageName: String? = nil) {
        
        messageLabel.text = text
        
        guard let imageNamed = imageName else{
            startAnimation()
            return
        }
        iconView.hidden = true
        maskIconView.hidden = true
        homeIconView.image = UIImage(named: imageNamed)
        
        
    }
    
    ///开始动画
    func startAnimation() {
        //transform.rotation
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        iconView.layer .addAnimation(anim, forKey: "rotation")
    }
    
    
    
    //MARK: - 私有控件
    //图片
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //小房子视图
    private lazy var homeIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //遮罩视图
    private lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //文字
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        //        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        label.numberOfLines = 0
        
        return label
    }()
    //注册按钮
    private lazy var registerButton : UIButton = {
        let button = UIButton()
        
        //        button.titleLabel?.text = "注册"
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return button
        
    }()
    
    //登录按钮
    private lazy var loginButton : UIButton = {
        let button = UIButton()
        //不能用这个方法设置,无法显示
        //        button.titleLabel?.text = "登录"
        
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        return button
        
    }()
    
    
}
//MARK: - 设置主界面
extension VistorView {
    func setUpUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(iconView)
            make.centerX.equalTo(iconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom)
            make.centerX.equalTo(iconView)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
}



